<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">
<meta name="referrer" content="same-origin">
<meta name="referrer" content="no-referrer" />








<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="RocketMQ之旅," />










<meta name="description" content="官方示例&amp;emsp;&amp;emsp;先看一下Consumer的类图结构：Consumer按照拉取的方式分为MQPullConsumer和MQPushConsumer，先从一个官网的MQPushConsumer示例开始：1234567891011121314151617181920212223242526272829public class Consumer &amp;#123;    public stati">
<meta name="keywords" content="RocketMQ之旅">
<meta property="og:type" content="article">
<meta property="og:title" content="RocketMQ之旅（四）client之Consumer">
<meta property="og:url" content="http://yoursite.com/2018/11/12/RocketMQ之旅（四）client之Consumer/index.html">
<meta property="og:site_name" content="矩阵编程">
<meta property="og:description" content="官方示例&amp;emsp;&amp;emsp;先看一下Consumer的类图结构：Consumer按照拉取的方式分为MQPullConsumer和MQPushConsumer，先从一个官网的MQPushConsumer示例开始：1234567891011121314151617181920212223242526272829public class Consumer &amp;#123;    public stati">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://ww1.sinaimg.cn/large/639cfad5ly1fwno96uqyvj20ta0j20wf.jpg">
<meta property="og:updated_time" content="2019-10-24T16:22:00.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="RocketMQ之旅（四）client之Consumer">
<meta name="twitter:description" content="官方示例&amp;emsp;&amp;emsp;先看一下Consumer的类图结构：Consumer按照拉取的方式分为MQPullConsumer和MQPushConsumer，先从一个官网的MQPushConsumer示例开始：1234567891011121314151617181920212223242526272829public class Consumer &amp;#123;    public stati">
<meta name="twitter:image" content="http://ww1.sinaimg.cn/large/639cfad5ly1fwno96uqyvj20ta0j20wf.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/11/12/RocketMQ之旅（四）client之Consumer/"/>





  <title>RocketMQ之旅（四）client之Consumer | 矩阵编程</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?7d29e6fba8e07d1a8af2414a20e5f2a6";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">矩阵编程</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-主页">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-主页"></i> <br />
            
            主页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-分类"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-所有文章">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-文章"></i> <br />
            
            所有文章
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/11/12/RocketMQ之旅（四）client之Consumer/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="matrix">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="矩阵编程">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">RocketMQ之旅（四）client之Consumer</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-11-12T15:54:12+08:00">
                2018-11-12
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/RocketMQ之旅/" itemprop="url" rel="index">
                    <span itemprop="name">RocketMQ之旅</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2018/11/12/RocketMQ之旅（四）client之Consumer/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2018/11/12/RocketMQ之旅（四）client之Consumer/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h4 id="官方示例"><a href="#官方示例" class="headerlink" title="官方示例"></a>官方示例</h4><p>&emsp;&emsp;先看一下Consumer的类图结构：<br><img src="http://ww1.sinaimg.cn/large/639cfad5ly1fwno96uqyvj20ta0j20wf.jpg" alt><br>Consumer按照拉取的方式分为MQPullConsumer和MQPushConsumer，先从一个官网的MQPushConsumer示例开始：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Consumer</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> InterruptedException, MQClientException </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// Instantiate with specified consumer group name.</span></span><br><span class="line">        DefaultMQPushConsumer consumer = <span class="keyword">new</span> DefaultMQPushConsumer(<span class="string">"please_rename_unique_group_name"</span>);</span><br><span class="line">         </span><br><span class="line">        <span class="comment">// Specify name server addresses.</span></span><br><span class="line">        consumer.setNamesrvAddr(<span class="string">"localhost:9876"</span>);</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// Subscribe one more more topics to consume.</span></span><br><span class="line">        consumer.subscribe(<span class="string">"TopicTest"</span>, <span class="string">"*"</span>);</span><br><span class="line">        <span class="comment">// Register callback to execute on arrival of messages fetched from brokers.</span></span><br><span class="line">        consumer.registerMessageListener(<span class="keyword">new</span> MessageListenerConcurrently() &#123;</span><br><span class="line"></span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> ConsumeConcurrentlyStatus <span class="title">consumeMessage</span><span class="params">(List&lt;MessageExt&gt; msgs,</span></span></span><br><span class="line"><span class="function"><span class="params">                ConsumeConcurrentlyContext context)</span> </span>&#123;</span><br><span class="line">                System.out.printf(<span class="string">"%s Receive New Messages: %s %n"</span>, Thread.currentThread().getName(), msgs);</span><br><span class="line">                <span class="keyword">return</span> ConsumeConcurrentlyStatus.CONSUME_SUCCESS;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//Launch the consumer instance.</span></span><br><span class="line">        consumer.start();</span><br><span class="line"></span><br><span class="line">        System.out.printf(<span class="string">"Consumer Started.%n"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<a id="more"></a>
<p>DefaultMQPushConsumer是MQPushConsumer的默认实现，看一下构造函数：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">DefaultMQPushConsumer</span><span class="params">(<span class="keyword">final</span> String consumerGroup)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>(consumerGroup, <span class="keyword">null</span>, <span class="keyword">new</span> AllocateMessageQueueAveragely());</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">DefaultMQPushConsumer</span><span class="params">(<span class="keyword">final</span> String consumerGroup, RPCHook rpcHook,</span></span></span><br><span class="line"><span class="function"><span class="params">    AllocateMessageQueueStrategy allocateMessageQueueStrategy)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.consumerGroup = consumerGroup;</span><br><span class="line">    <span class="keyword">this</span>.allocateMessageQueueStrategy = allocateMessageQueueStrategy;</span><br><span class="line">    defaultMQPushConsumerImpl = <span class="keyword">new</span> DefaultMQPushConsumerImpl(<span class="keyword">this</span>, rpcHook);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><strong>AllocateMessageQueueStrategy</strong>是一个消息队列分配的策略，有下面几种方式：</p>
<ol>
<li><strong>AllocateMachineRoomNearby</strong> 机房就近分配</li>
<li><strong>AllocateMessageQueueAveragely</strong> 均分</li>
<li><strong>AllocateMessageQueueAveragelyByCircle</strong> 环形均分</li>
<li><strong>AllocateMessageQueueByConfig</strong> 根据配置进行分配</li>
<li><strong>AllocateMessageQueueByMachineRoom</strong> 机房均分</li>
<li><strong>AllocateMessageQueueConsistentHash</strong> 一致性hash</li>
</ol>
<p>这里默认的策略是<strong>AllocateMessageQueueAveragely</strong>，举一反三，我们看一下<strong>AllocateMessageQueueAveragely</strong>是如何进行消息队列分配的：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> List&lt;MessageQueue&gt; <span class="title">allocate</span><span class="params">(String consumerGroup, String currentCID, List&lt;MessageQueue&gt; mqAll,</span></span></span><br><span class="line"><span class="function"><span class="params">    List&lt;String&gt; cidAll)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// ...省略上部分代码...</span></span><br><span class="line">    <span class="keyword">int</span> index = cidAll.indexOf(currentCID);</span><br><span class="line">    <span class="keyword">int</span> mod = mqAll.size() % cidAll.size();</span><br><span class="line">    <span class="keyword">int</span> averageSize =</span><br><span class="line">        mqAll.size() &lt;= cidAll.size() ? <span class="number">1</span> : (mod &gt; <span class="number">0</span> &amp;&amp; index &lt; mod ? mqAll.size() / cidAll.size()</span><br><span class="line">            + <span class="number">1</span> : mqAll.size() / cidAll.size());</span><br><span class="line">    <span class="keyword">int</span> startIndex = (mod &gt; <span class="number">0</span> &amp;&amp; index &lt; mod) ? index * averageSize : index * averageSize + mod;</span><br><span class="line">    <span class="keyword">int</span> range = Math.min(averageSize, mqAll.size() - startIndex);</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; range; i++) &#123;</span><br><span class="line">        result.add(mqAll.get((startIndex + i) % mqAll.size()));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<ol>
<li><p>先说一下方法的参数：</p>
<ul>
<li>consumerGroup 消费组，如上每次创建一个DefaultMQPushConsumer都会给一个consumerGroup名称。</li>
<li>currentCID 当前消费者的id</li>
<li>mqAll 当前topic的消息队列</li>
<li>cidAll 当前消费者组所有消费者的id集合</li>
</ul>
</li>
<li><p>这个算法有点绕，简单点说，假设有7个消息队列，3个消费者，那么每个消费者平均分配 7 / 3 = 2 个消息队列，剩余消息队列 7 % 3 = 1 由头结点瓜分，消息队列分配结果分别是：(0,1,2)，(3,4)，(5,6)。</p>
</li>
</ol>
<p>&emsp;&emsp;继续看示例，DefaultMQPushConsumer的subscribe做了什么：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">subscribe</span><span class="params">(String topic, String subExpression)</span> <span class="keyword">throws</span> MQClientException </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.defaultMQPushConsumerImpl.subscribe(topic, subExpression);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>DefaultMQPushConsumer的实现和DefaultMQProducer类似，他们的方法都由一个内部的代理去实现，DefaultMQPushConsumer的多数实现都交给DefaultMQPushConsumerImpl。所以可以看到这里DefaultMQPushConsumer的<strong>subscribe()</strong>实际调用了DefaultMQPushConsumerImpl的实现：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">subscribe</span><span class="params">(String topic, String subExpression)</span> <span class="keyword">throws</span> MQClientException </span>&#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        SubscriptionData subscriptionData = FilterAPI.buildSubscriptionData(<span class="keyword">this</span>.defaultMQPushConsumer.getConsumerGroup(),</span><br><span class="line">            topic, subExpression);</span><br><span class="line">        <span class="keyword">this</span>.rebalanceImpl.getSubscriptionInner().put(topic, subscriptionData);</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">this</span>.mQClientFactory != <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">this</span>.mQClientFactory.sendHeartbeatToAllBrokerWithLock();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> MQClientException(<span class="string">"subscription exception"</span>, e);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>由FilterAPI创建SubscriptionData，subExpression传入的是消费的tags，多个tags由||分割，可以针对同一个topic，针对不同的tags创建不同的消费者。示例里面传入的是*，并没有区分tags。</p>
<h4 id="DefaultMQPushConsumerImpl启动"><a href="#DefaultMQPushConsumerImpl启动" class="headerlink" title="DefaultMQPushConsumerImpl启动"></a>DefaultMQPushConsumerImpl启动</h4><p>&emsp;&emsp;继续看示例，DefaultMQPushConsumer注册了一个消息监听的匿名类，然后调用<strong>start()</strong>，当然这里真实的start动作还是交由DefaultMQPushConsumerImpl代理：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">synchronized</span> <span class="keyword">void</span> <span class="title">start</span><span class="params">()</span> <span class="keyword">throws</span> MQClientException </span>&#123;</span><br><span class="line">    <span class="keyword">switch</span> (<span class="keyword">this</span>.serviceState) &#123;</span><br><span class="line">        <span class="keyword">case</span> CREATE_JUST:</span><br><span class="line">            <span class="comment">// （1）第一部分代码 初始化一些必要类并设置参数</span></span><br><span class="line">            <span class="comment">// （2）第二部分代码 存储策略</span></span><br><span class="line">            <span class="comment">// （3）第三部分代码 根据消费者注册的监听类初始化不同的监听服务</span></span><br><span class="line">            <span class="comment">// （4）第四部分代码 启动 MQClientInstance</span></span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="comment">// 省略其他case</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// （五）第五部分代码 MQClientInstance检测Broker发送心跳，设置负载均衡</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>为了方便查阅，把<strong>start()</strong>方法解剖成四个部分。第一部分主要初始化一些必要的类和参数：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">this</span>.serviceState = ServiceState.START_FAILED;</span><br><span class="line"><span class="keyword">this</span>.checkConfig();</span><br><span class="line"><span class="keyword">this</span>.copySubscription();</span><br><span class="line"><span class="keyword">if</span> (<span class="keyword">this</span>.defaultMQPushConsumer.getMessageModel() == MessageModel.CLUSTERING) &#123;</span><br><span class="line">    <span class="keyword">this</span>.defaultMQPushConsumer.changeInstanceNameToPID();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">this</span>.mQClientFactory = MQClientManager.getInstance().getAndCreateMQClientInstance(<span class="keyword">this</span>.defaultMQPushConsumer, <span class="keyword">this</span>.rpcHook);</span><br><span class="line"><span class="comment">// rebalanceImpl初始化参数设置</span></span><br><span class="line"><span class="keyword">this</span>.rebalanceImpl.setConsumerGroup(<span class="keyword">this</span>.defaultMQPushConsumer.getConsumerGroup());</span><br><span class="line"><span class="keyword">this</span>.rebalanceImpl.setMessageModel(<span class="keyword">this</span>.defaultMQPushConsumer.getMessageModel());</span><br><span class="line"><span class="keyword">this</span>.rebalanceImpl.setAllocateMessageQueueStrategy(<span class="keyword">this</span>.defaultMQPushConsumer.getAllocateMessageQueueStrategy());</span><br><span class="line"><span class="keyword">this</span>.rebalanceImpl.setmQClientFactory(<span class="keyword">this</span>.mQClientFactory);</span><br><span class="line"></span><br><span class="line"><span class="keyword">this</span>.pullAPIWrapper = <span class="keyword">new</span> PullAPIWrapper(</span><br><span class="line">    mQClientFactory,</span><br><span class="line">    <span class="keyword">this</span>.defaultMQPushConsumer.getConsumerGroup(), isUnitMode());</span><br><span class="line"><span class="keyword">this</span>.pullAPIWrapper.registerFilterMessageHook(filterMessageHookList);</span><br></pre></td></tr></table></figure></p>
<ol>
<li><strong>checkConfig()</strong>检测配置，<strong>copySubscription()</strong>新建消费者的SubscriptionData，如果你调用<strong>setSubscription()</strong>设置了消费者。</li>
<li>调用MQClientManager新建MQClientInstance，和DefaultMQProducerImpl中的<strong>start()</strong>做的事如出一辙。在MQClientInstance初始化了拉取消息的服务：<strong>PullMessageService</strong>。</li>
<li><strong>PullAPIWrapper</strong> 负责拉取消息</li>
</ol>
<p>看一下第二部分的代码，根据不同消费形式初始化存储策略：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (<span class="keyword">this</span>.defaultMQPushConsumer.getOffsetStore() != <span class="keyword">null</span>) &#123;</span><br><span class="line">    <span class="keyword">this</span>.offsetStore = <span class="keyword">this</span>.defaultMQPushConsumer.getOffsetStore();</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">switch</span> (<span class="keyword">this</span>.defaultMQPushConsumer.getMessageModel()) &#123;</span><br><span class="line">        <span class="keyword">case</span> BROADCASTING:</span><br><span class="line">            <span class="keyword">this</span>.offsetStore = <span class="keyword">new</span> LocalFileOffsetStore(<span class="keyword">this</span>.mQClientFactory, <span class="keyword">this</span>.defaultMQPushConsumer.getConsumerGroup());</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> CLUSTERING:</span><br><span class="line">            <span class="keyword">this</span>.offsetStore = <span class="keyword">new</span> RemoteBrokerOffsetStore(<span class="keyword">this</span>.mQClientFactory, <span class="keyword">this</span>.defaultMQPushConsumer.getConsumerGroup());</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">this</span>.defaultMQPushConsumer.setOffsetStore(<span class="keyword">this</span>.offsetStore);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">this</span>.offsetStore.load();</span><br></pre></td></tr></table></figure></p>
<p>RocketMQ支持两种消费者形式，在<strong>MessageModel</strong>中定义：</p>
<ul>
<li>BROADCASTING 广播消费</li>
<li>CLUSTERING 集群消费<br>如果你设置了CLUSTERING，那么默认的client名称会被设置为PID。根据不同的消费模式，还会选择不一样的存储方式，BROADCASTING使用<strong>LocalFileOffsetStore</strong>，CLUSTERING使用<strong>RemoteBrokerOffsetStore</strong>存储消费进度，关于Store模块留作后续讨论。</li>
</ul>
<p>继续看第三部分代码，示例里面消费者registerMessageListener注册了一个匿名内部类，根据不同的监听器会初始化不同的监听服务：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (<span class="keyword">this</span>.getMessageListenerInner() <span class="keyword">instanceof</span> MessageListenerOrderly) &#123;</span><br><span class="line">    <span class="keyword">this</span>.consumeOrderly = <span class="keyword">true</span>;</span><br><span class="line">    <span class="keyword">this</span>.consumeMessageService =</span><br><span class="line">        <span class="keyword">new</span> ConsumeMessageOrderlyService(<span class="keyword">this</span>, (MessageListenerOrderly) <span class="keyword">this</span>.getMessageListenerInner());</span><br><span class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="keyword">this</span>.getMessageListenerInner() <span class="keyword">instanceof</span> MessageListenerConcurrently) &#123;</span><br><span class="line">    <span class="keyword">this</span>.consumeOrderly = <span class="keyword">false</span>;</span><br><span class="line">    <span class="keyword">this</span>.consumeMessageService =</span><br><span class="line">        <span class="keyword">new</span> ConsumeMessageConcurrentlyService(<span class="keyword">this</span>, (MessageListenerConcurrently) <span class="keyword">this</span>.getMessageListenerInner());</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">this</span>.consumeMessageService.start();</span><br></pre></td></tr></table></figure></p>
<p>先来看下<strong>ConsumeMessageOrderlyService</strong>的<strong>start()</strong>:<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">start</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (MessageModel.CLUSTERING.equals(ConsumeMessageOrderlyService.<span class="keyword">this</span>.defaultMQPushConsumerImpl.messageModel())) &#123;</span><br><span class="line">        <span class="keyword">this</span>.scheduledExecutorService.scheduleAtFixedRate(<span class="keyword">new</span> Runnable() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                ConsumeMessageOrderlyService.<span class="keyword">this</span>.lockMQPeriodically();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;, <span class="number">1000</span> * <span class="number">1</span>, ProcessQueue.REBALANCE_LOCK_INTERVAL, TimeUnit.MILLISECONDS);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>启动了一个定时任务，延迟一秒时间，默认每20s执行一次，执行rebalanceImpl的<strong>lockAll()</strong>：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">synchronized</span> <span class="keyword">void</span> <span class="title">lockMQPeriodically</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (!<span class="keyword">this</span>.stopped) &#123;</span><br><span class="line">        <span class="keyword">this</span>.defaultMQPushConsumerImpl.getRebalanceImpl().lockAll();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>看一下<strong>RebalanceImpl</strong>的<strong>lockAll()</strong>：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">lockAll</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    HashMap&lt;String, Set&lt;MessageQueue&gt;&gt; brokerMqs = <span class="keyword">this</span>.buildProcessQueueTableByBrokerName();</span><br><span class="line"></span><br><span class="line">    Iterator&lt;Entry&lt;String, Set&lt;MessageQueue&gt;&gt;&gt; it = brokerMqs.entrySet().iterator();</span><br><span class="line">    <span class="keyword">while</span> (it.hasNext()) &#123;</span><br><span class="line">        Entry&lt;String, Set&lt;MessageQueue&gt;&gt; entry = it.next();</span><br><span class="line">        <span class="keyword">final</span> String brokerName = entry.getKey();</span><br><span class="line">        <span class="keyword">final</span> Set&lt;MessageQueue&gt; mqs = entry.getValue();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (mqs.isEmpty())</span><br><span class="line">            <span class="keyword">continue</span>;</span><br><span class="line"></span><br><span class="line">        FindBrokerResult findBrokerResult = <span class="keyword">this</span>.mQClientFactory.findBrokerAddressInSubscribe(brokerName, MixAll.MASTER_ID, <span class="keyword">true</span>);</span><br><span class="line">        <span class="keyword">if</span> (findBrokerResult != <span class="keyword">null</span>) &#123;</span><br><span class="line">            LockBatchRequestBody requestBody = <span class="keyword">new</span> LockBatchRequestBody();</span><br><span class="line">            requestBody.setConsumerGroup(<span class="keyword">this</span>.consumerGroup);</span><br><span class="line">            requestBody.setClientId(<span class="keyword">this</span>.mQClientFactory.getClientId());</span><br><span class="line">            requestBody.setMqSet(mqs);</span><br><span class="line"></span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                Set&lt;MessageQueue&gt; lockOKMQSet =</span><br><span class="line">                    <span class="keyword">this</span>.mQClientFactory.getMQClientAPIImpl().lockBatchMQ(findBrokerResult.getBrokerAddr(), requestBody, <span class="number">1000</span>);</span><br><span class="line"></span><br><span class="line">                <span class="keyword">for</span> (MessageQueue mq : lockOKMQSet) &#123;</span><br><span class="line">                    ProcessQueue processQueue = <span class="keyword">this</span>.processQueueTable.get(mq);</span><br><span class="line">                    <span class="keyword">if</span> (processQueue != <span class="keyword">null</span>) &#123;</span><br><span class="line">                        <span class="keyword">if</span> (!processQueue.isLocked()) &#123;</span><br><span class="line">                            log.info(<span class="string">"the message queue locked OK, Group: &#123;&#125; &#123;&#125;"</span>, <span class="keyword">this</span>.consumerGroup, mq);</span><br><span class="line">                        &#125;</span><br><span class="line"></span><br><span class="line">                        processQueue.setLocked(<span class="keyword">true</span>);</span><br><span class="line">                        processQueue.setLastLockTimestamp(System.currentTimeMillis());</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">for</span> (MessageQueue mq : mqs) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (!lockOKMQSet.contains(mq)) &#123;</span><br><span class="line">                        ProcessQueue processQueue = <span class="keyword">this</span>.processQueueTable.get(mq);</span><br><span class="line">                        <span class="keyword">if</span> (processQueue != <span class="keyword">null</span>) &#123;</span><br><span class="line">                            processQueue.setLocked(<span class="keyword">false</span>);</span><br><span class="line">                            log.warn(<span class="string">"the message queue locked Failed, Group: &#123;&#125; &#123;&#125;"</span>, <span class="keyword">this</span>.consumerGroup, mq);</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                log.error(<span class="string">"lockBatchMQ exception, "</span> + mqs, e);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>给Borker发送批量锁定MessageQueue的指令，当然，锁定只是在Broker中标记一下MessageQueue，保证broker中的每個消息队列只对应一个消费端（顺序消费模式下为了保消息的顺序消费），另外如果在broker中锁定成功，同时也会在本地consumer端设置ProcessQueue的locked为true。再来看一下另外一个监听处理器<strong>ConsumeMessageConcurrentlyService</strong>的<strong>start()</strong>：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">start</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.cleanExpireMsgExecutors.scheduleAtFixedRate(<span class="keyword">new</span> Runnable() &#123;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            cleanExpireMsg();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;, <span class="keyword">this</span>.defaultMQPushConsumer.getConsumeTimeout(), <span class="keyword">this</span>.defaultMQPushConsumer.getConsumeTimeout(), TimeUnit.MINUTES);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>也是一个定时任务，默认每15分钟执行一次<strong>cleanExpireMsg()</strong>：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">cleanExpireMsg</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    Iterator&lt;Map.Entry&lt;MessageQueue, ProcessQueue&gt;&gt; it =</span><br><span class="line">        <span class="keyword">this</span>.defaultMQPushConsumerImpl.getRebalanceImpl().getProcessQueueTable().entrySet().iterator();</span><br><span class="line">    <span class="keyword">while</span> (it.hasNext()) &#123;</span><br><span class="line">        Map.Entry&lt;MessageQueue, ProcessQueue&gt; next = it.next();</span><br><span class="line">        ProcessQueue pq = next.getValue();</span><br><span class="line">        pq.cleanExpiredMsg(<span class="keyword">this</span>.defaultMQPushConsumer);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>ProcessQueue是队列消费处理快照，看一下它的<strong>cleanExpiredMsg()</strong>：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">cleanExpiredMsg</span><span class="params">(DefaultMQPushConsumer pushConsumer)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (pushConsumer.getDefaultMQPushConsumerImpl().isConsumeOrderly()) &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">int</span> loop = msgTreeMap.size() &lt; <span class="number">16</span> ? msgTreeMap.size() : <span class="number">16</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; loop; i++) &#123;</span><br><span class="line">        MessageExt msg = <span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="keyword">this</span>.lockTreeMap.readLock().lockInterruptibly();</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                <span class="keyword">if</span> (!msgTreeMap.isEmpty() &amp;&amp; System.currentTimeMillis() - Long.parseLong(MessageAccessor.getConsumeStartTimeStamp(msgTreeMap.firstEntry().getValue())) &gt; pushConsumer.getConsumeTimeout() * <span class="number">60</span> * <span class="number">1000</span>) &#123;</span><br><span class="line">                    msg = msgTreeMap.firstEntry().getValue();</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line"></span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">                <span class="keyword">this</span>.lockTreeMap.readLock().unlock();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">            log.error(<span class="string">"getExpiredMsg exception"</span>, e);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line"></span><br><span class="line">            pushConsumer.sendMessageBack(msg, <span class="number">3</span>);</span><br><span class="line">            log.info(<span class="string">"send expire msg back. topic=&#123;&#125;, msgId=&#123;&#125;, storeHost=&#123;&#125;, queueId=&#123;&#125;, queueOffset=&#123;&#125;"</span>, msg.getTopic(), msg.getMsgId(), msg.getStoreHost(), msg.getQueueId(), msg.getQueueOffset());</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                <span class="keyword">this</span>.lockTreeMap.writeLock().lockInterruptibly();</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    <span class="keyword">if</span> (!msgTreeMap.isEmpty() &amp;&amp; msg.getQueueOffset() == msgTreeMap.firstKey()) &#123;</span><br><span class="line">                        <span class="keyword">try</span> &#123;</span><br><span class="line">                            removeMessage(Collections.singletonList(msg));</span><br><span class="line">                        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                            log.error(<span class="string">"send expired msg exception"</span>, e);</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">                    <span class="keyword">this</span>.lockTreeMap.writeLock().unlock();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                log.error(<span class="string">"getExpiredMsg exception"</span>, e);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            log.error(<span class="string">"send expired msg exception"</span>, e);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>为了避免消息消费超时而一直卡在这里，cleanExpiredMsg会清除消费超过15分钟的消息，触发sendBack把消息发送回Broker。跳过消息监听器，继续看<strong>DefaultMQPushConsumerImpl</strong>的<strong>start()</strong>。第四部分代码，启动MQClientInstance和之前Producer中的一致，所以不再赘述，直接看第五部分代码：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 更新Topic订阅者信息</span></span><br><span class="line"><span class="keyword">this</span>.updateTopicSubscribeInfoWhenSubscriptionChanged();</span><br><span class="line"><span class="comment">// 检测集群中的broker配置一致</span></span><br><span class="line"><span class="keyword">this</span>.mQClientFactory.checkClientInBroker();</span><br><span class="line"><span class="comment">// 发送心跳信息</span></span><br><span class="line"><span class="keyword">this</span>.mQClientFactory.sendHeartbeatToAllBrokerWithLock();</span><br><span class="line"><span class="comment">// 立即重启负载均衡线程</span></span><br><span class="line"><span class="keyword">this</span>.mQClientFactory.rebalanceImmediately();</span><br></pre></td></tr></table></figure></p>
<p><strong>rebalanceImmediately()</strong>其实就是给消费者分配消息队列，CLUSTERING模式下使用上面说过的<strong>AllocateMessageQueueStrategy</strong>分配消息队列。</p>
<h4 id="拉取消息"><a href="#拉取消息" class="headerlink" title="拉取消息"></a>拉取消息</h4><p>&emsp;&emsp;上面说了整个<strong>DefaultMQPushConsumer</strong>的启动过程，但是仍然有一个问题，就是<strong>DefaultMQPushConsumer</strong>究竟是如何获取并处理消息的？答案就在MQClientInstance中初始化的<strong>PullMessageService</strong>。<strong>PullMessageService</strong>继承于<strong>ServiceThread</strong>，是RocketMQ封装的线程服务，<strong>PullMessageService</strong>就是一个线程，看一下他的<strong>run()</strong>：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    log.info(<span class="keyword">this</span>.getServiceName() + <span class="string">" service started"</span>);</span><br><span class="line">    <span class="keyword">while</span> (!<span class="keyword">this</span>.isStopped()) &#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            PullRequest pullRequest = <span class="keyword">this</span>.pullRequestQueue.take();</span><br><span class="line">            <span class="keyword">this</span>.pullMessage(pullRequest);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (InterruptedException ignored) &#123;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            log.error(<span class="string">"Pull Message Service Run Method exception"</span>, e);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    log.info(<span class="keyword">this</span>.getServiceName() + <span class="string">" service end"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><strong>pullRequestQueue</strong>是一个<strong>PullRequest</strong>的阻塞队列，看一下<strong>pullMessage()</strong>：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">pullMessage</span><span class="params">(<span class="keyword">final</span> PullRequest pullRequest)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">final</span> MQConsumerInner consumer = <span class="keyword">this</span>.mQClientFactory.selectConsumer(pullRequest.getConsumerGroup());</span><br><span class="line">    <span class="keyword">if</span> (consumer != <span class="keyword">null</span>) &#123;</span><br><span class="line">        DefaultMQPushConsumerImpl impl = (DefaultMQPushConsumerImpl) consumer;</span><br><span class="line">        impl.pullMessage(pullRequest);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        log.warn(<span class="string">"No matched consumer for the PullRequest &#123;&#125;, drop it"</span>, pullRequest);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>调用了<strong>DefaultMQPushConsumerImpl</strong>的<strong>pullMessage()</strong>：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">pullMessage</span><span class="params">(<span class="keyword">final</span> PullRequest pullRequest)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 1. 检测消费者状态、消息缓存情况</span></span><br><span class="line">    <span class="comment">// 2. 设置PullCallback</span></span><br><span class="line">    <span class="comment">// 3. 调用PullAPIWrapper拉取消息</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>简单说一下消费者状态、缓存和锁的检测：</p>
<ol>
<li>消费者拉取消息的时候，会查看消息缓存的消息数量、大小，默认如果消息数量超过<strong>pullThresholdForQueue</strong>（默认1000），大小超过<strong>pullThresholdSizeForQueue</strong>（默认100M），此PullRequest请求不会执行，会被重新放入阻塞队列。</li>
<li>如果是乱序消费消息的情况，拉取消息的时候还会进行流控处理，限制单个队列并行消费的跨度，如果大于<strong>consumeConcurrentlyMaxSpan</strong>（默认2000），也会触发延时请求，将PullRequest重新放入阻塞队列并延迟50ms请求。</li>
<li>如果是顺序消费消息的情况，还会检测<strong>ProcessQueue</strong>的locked状态，如果locked为true并且PullRequest的lockedFirst属性为false，则会根据上次消息消费的进度执行PullRequest请求。上面说过，<strong>RebalanceImpl</strong>的<strong>lockAll()</strong>会周期性地获取MessageQueue的分布式锁，并将<strong>RebalanceImpl</strong>中的<strong>processQueueTable</strong>（类型为ConcurrentHashMap &lt;MessageQueue, ProcessQueue&gt;）集合中获得分布式锁的MessageQueue对应的ProcessQueue对象加上本地锁，并记录加锁的时间戳（即设置该对象的lock为ture、记录lastLockTimestamp）。</li>
</ol>
<p>再来说下<strong>PullCallback</strong>的设置：</p>
<ol>
<li>成功的话会返回一个<strong>PullStatus</strong>，分为四种情况：<strong>FOUND</strong>（有新消息）、<strong>NO_NEW_MSG</strong>（无新消息）、<strong>NO_MATCHED_MSG</strong>（过滤后没有对应的消息）、<strong>OFFSET_ILLEGAL</strong>（非法的消息索引进度），根据不同的<strong>PullStatus</strong>进行对应的处理。</li>
<li><p>看一下<strong>FOUND</strong>分支的处理：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">long</span> prevRequestOffset = pullRequest.getNextOffset();</span><br><span class="line">pullRequest.setNextOffset(pullResult.getNextBeginOffset());</span><br><span class="line"><span class="keyword">long</span> pullRT = System.currentTimeMillis() - beginTimestamp;</span><br><span class="line">DefaultMQPushConsumerImpl.<span class="keyword">this</span>.getConsumerStatsManager().incPullRT(pullRequest.getConsumerGroup(),</span><br><span class="line">    pullRequest.getMessageQueue().getTopic(), pullRT);</span><br><span class="line"></span><br><span class="line"><span class="keyword">long</span> firstMsgOffset = Long.MAX_VALUE;</span><br><span class="line"><span class="keyword">if</span> (pullResult.getMsgFoundList() == <span class="keyword">null</span> || pullResult.getMsgFoundList().isEmpty()) &#123;</span><br><span class="line">    DefaultMQPushConsumerImpl.<span class="keyword">this</span>.executePullRequestImmediately(pullRequest);</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    firstMsgOffset = pullResult.getMsgFoundList().get(<span class="number">0</span>).getQueueOffset();</span><br><span class="line"></span><br><span class="line">    DefaultMQPushConsumerImpl.<span class="keyword">this</span>.getConsumerStatsManager().incPullTPS(pullRequest.getConsumerGroup(),</span><br><span class="line">        pullRequest.getMessageQueue().getTopic(), pullResult.getMsgFoundList().size());</span><br><span class="line"></span><br><span class="line">    <span class="keyword">boolean</span> dispatchToConsume = processQueue.putMessage(pullResult.getMsgFoundList());</span><br><span class="line">    DefaultMQPushConsumerImpl.<span class="keyword">this</span>.consumeMessageService.submitConsumeRequest(</span><br><span class="line">        pullResult.getMsgFoundList(),</span><br><span class="line">        processQueue,</span><br><span class="line">        pullRequest.getMessageQueue(),</span><br><span class="line">        dispatchToConsume);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (DefaultMQPushConsumerImpl.<span class="keyword">this</span>.defaultMQPushConsumer.getPullInterval() &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        DefaultMQPushConsumerImpl.<span class="keyword">this</span>.executePullRequestLater(pullRequest,</span><br><span class="line">            DefaultMQPushConsumerImpl.<span class="keyword">this</span>.defaultMQPushConsumer.getPullInterval());</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        DefaultMQPushConsumerImpl.<span class="keyword">this</span>.executePullRequestImmediately(pullRequest);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (pullResult.getNextBeginOffset() &lt; prevRequestOffset</span><br><span class="line">    || firstMsgOffset &lt; prevRequestOffset) &#123;</span><br><span class="line">    log.warn(</span><br><span class="line">        <span class="string">"[BUG] pull message result maybe data wrong, nextBeginOffset: &#123;&#125; firstMsgOffset: &#123;&#125; prevRequestOffset: &#123;&#125;"</span>,</span><br><span class="line">        pullResult.getNextBeginOffset(),</span><br><span class="line">        firstMsgOffset,</span><br><span class="line">        prevRequestOffset);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>如果获取的消息列表为空，立即再次执行PullRequest拉取消息，否则提交消费回执，并再次提交PullRequest到阻塞队列。</p>
</li>
</ol>
<p>最后调用PullAPIWrapper拉取消息：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">this</span>.pullAPIWrapper.pullKernelImpl(</span><br><span class="line">    pullRequest.getMessageQueue(),</span><br><span class="line">    subExpression,</span><br><span class="line">    subscriptionData.getExpressionType(),</span><br><span class="line">    subscriptionData.getSubVersion(),</span><br><span class="line">    pullRequest.getNextOffset(),</span><br><span class="line">    <span class="keyword">this</span>.defaultMQPushConsumer.getPullBatchSize(),</span><br><span class="line">    sysFlag,</span><br><span class="line">    commitOffsetValue,</span><br><span class="line">    BROKER_SUSPEND_MAX_TIME_MILLIS,</span><br><span class="line">    CONSUMER_TIMEOUT_MILLIS_WHEN_SUSPEND,</span><br><span class="line">    CommunicationMode.ASYNC,</span><br><span class="line">    pullCallback</span><br></pre></td></tr></table></figure></p>
<p>看一下<strong>PullAPIWrapper</strong>的<strong>pullKernelImpl()</strong>：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> PullResult <span class="title">pullKernelImpl</span><span class="params">(</span></span></span><br><span class="line"><span class="function"><span class="params">    <span class="keyword">final</span> MessageQueue mq,</span></span></span><br><span class="line"><span class="function"><span class="params">    <span class="keyword">final</span> String subExpression,</span></span></span><br><span class="line"><span class="function"><span class="params">    <span class="keyword">final</span> String expressionType,</span></span></span><br><span class="line"><span class="function"><span class="params">    <span class="keyword">final</span> <span class="keyword">long</span> subVersion,</span></span></span><br><span class="line"><span class="function"><span class="params">    <span class="keyword">final</span> <span class="keyword">long</span> offset,</span></span></span><br><span class="line"><span class="function"><span class="params">    <span class="keyword">final</span> <span class="keyword">int</span> maxNums,</span></span></span><br><span class="line"><span class="function"><span class="params">    <span class="keyword">final</span> <span class="keyword">int</span> sysFlag,</span></span></span><br><span class="line"><span class="function"><span class="params">    <span class="keyword">final</span> <span class="keyword">long</span> commitOffset,</span></span></span><br><span class="line"><span class="function"><span class="params">    <span class="keyword">final</span> <span class="keyword">long</span> brokerSuspendMaxTimeMillis,</span></span></span><br><span class="line"><span class="function"><span class="params">    <span class="keyword">final</span> <span class="keyword">long</span> timeoutMillis,</span></span></span><br><span class="line"><span class="function"><span class="params">    <span class="keyword">final</span> CommunicationMode communicationMode,</span></span></span><br><span class="line"><span class="function"><span class="params">    <span class="keyword">final</span> PullCallback pullCallback</span></span></span><br><span class="line"><span class="function"><span class="params">)</span> <span class="keyword">throws</span> MQClientException, RemotingException, MQBrokerException, InterruptedException </span>&#123;</span><br><span class="line">    FindBrokerResult findBrokerResult =</span><br><span class="line">        <span class="keyword">this</span>.mQClientFactory.findBrokerAddressInSubscribe(mq.getBrokerName(),</span><br><span class="line">            <span class="keyword">this</span>.recalculatePullFromWhichNode(mq), <span class="keyword">false</span>);</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">null</span> == findBrokerResult) &#123;</span><br><span class="line">        <span class="keyword">this</span>.mQClientFactory.updateTopicRouteInfoFromNameServer(mq.getTopic());</span><br><span class="line">        findBrokerResult =</span><br><span class="line">            <span class="keyword">this</span>.mQClientFactory.findBrokerAddressInSubscribe(mq.getBrokerName(),</span><br><span class="line">                <span class="keyword">this</span>.recalculatePullFromWhichNode(mq), <span class="keyword">false</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (findBrokerResult != <span class="keyword">null</span>) &#123;</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">// check version</span></span><br><span class="line">            <span class="keyword">if</span> (!ExpressionType.isTagType(expressionType)</span><br><span class="line">                &amp;&amp; findBrokerResult.getBrokerVersion() &lt; MQVersion.Version.V4_1_0_SNAPSHOT.ordinal()) &#123;</span><br><span class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> MQClientException(<span class="string">"The broker["</span> + mq.getBrokerName() + <span class="string">", "</span></span><br><span class="line">                    + findBrokerResult.getBrokerVersion() + <span class="string">"] does not upgrade to support for filter message by "</span> + expressionType, <span class="keyword">null</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> sysFlagInner = sysFlag;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (findBrokerResult.isSlave()) &#123;</span><br><span class="line">            sysFlagInner = PullSysFlag.clearCommitOffsetFlag(sysFlagInner);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        PullMessageRequestHeader requestHeader = <span class="keyword">new</span> PullMessageRequestHeader();</span><br><span class="line">        requestHeader.setConsumerGroup(<span class="keyword">this</span>.consumerGroup);</span><br><span class="line">        requestHeader.setTopic(mq.getTopic());</span><br><span class="line">        requestHeader.setQueueId(mq.getQueueId());</span><br><span class="line">        requestHeader.setQueueOffset(offset);</span><br><span class="line">        requestHeader.setMaxMsgNums(maxNums);</span><br><span class="line">        requestHeader.setSysFlag(sysFlagInner);</span><br><span class="line">        requestHeader.setCommitOffset(commitOffset);</span><br><span class="line">        requestHeader.setSuspendTimeoutMillis(brokerSuspendMaxTimeMillis);</span><br><span class="line">        requestHeader.setSubscription(subExpression);</span><br><span class="line">        requestHeader.setSubVersion(subVersion);</span><br><span class="line">        requestHeader.setExpressionType(expressionType);</span><br><span class="line"></span><br><span class="line">        String brokerAddr = findBrokerResult.getBrokerAddr();</span><br><span class="line">        <span class="keyword">if</span> (PullSysFlag.hasClassFilterFlag(sysFlagInner)) &#123;</span><br><span class="line">            brokerAddr = computPullFromWhichFilterServer(mq.getTopic(), brokerAddr);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        PullResult pullResult = <span class="keyword">this</span>.mQClientFactory.getMQClientAPIImpl().pullMessage(</span><br><span class="line">            brokerAddr,</span><br><span class="line">            requestHeader,</span><br><span class="line">            timeoutMillis,</span><br><span class="line">            communicationMode,</span><br><span class="line">            pullCallback);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> pullResult;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> MQClientException(<span class="string">"The broker["</span> + mq.getBrokerName() + <span class="string">"] not exist"</span>, <span class="keyword">null</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>Async模式拉取消息，接下来就交给netty处理了。以上我们其实都是围绕Push模式下的消息消费模式，但从代码上来看，其实Push模式实质上是一个阻塞队列拉取消息的过程。</p>
<h4 id="消费消息"><a href="#消费消息" class="headerlink" title="消费消息"></a>消费消息</h4><p>&emsp;&emsp;上面说过在<strong>DefaultMQPushConsumerImpl</strong>调用<strong>start()</strong>的时候，会获取对应的<strong>MQClientInstance</strong>，<strong>MQClientInstance</strong>初始化了一系列的客户端API，可以在Consumer和Producer之间共用。其中就有用于netty消息的发送和处理的类<strong>MQClientAPIImpl</strong>，它使用一个<strong>ClientRemotingProcessor</strong>注册了一些请求事件的处理器，看一下<strong>MQClientAPIImpl</strong>的构造函数：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">MQClientAPIImpl</span><span class="params">(<span class="keyword">final</span> NettyClientConfig nettyClientConfig,</span></span></span><br><span class="line"><span class="function"><span class="params">    <span class="keyword">final</span> ClientRemotingProcessor clientRemotingProcessor,</span></span></span><br><span class="line"><span class="function"><span class="params">    RPCHook rpcHook, <span class="keyword">final</span> ClientConfig clientConfig)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.clientConfig = clientConfig;</span><br><span class="line">    topAddressing = <span class="keyword">new</span> TopAddressing(MixAll.getWSAddr(), clientConfig.getUnitName());</span><br><span class="line">    <span class="keyword">this</span>.remotingClient = <span class="keyword">new</span> NettyRemotingClient(nettyClientConfig, <span class="keyword">null</span>);</span><br><span class="line">    <span class="keyword">this</span>.clientRemotingProcessor = clientRemotingProcessor;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>.remotingClient.registerRPCHook(rpcHook);</span><br><span class="line">    <span class="keyword">this</span>.remotingClient.registerProcessor(RequestCode.CHECK_TRANSACTION_STATE, <span class="keyword">this</span>.clientRemotingProcessor, <span class="keyword">null</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>.remotingClient.registerProcessor(RequestCode.NOTIFY_CONSUMER_IDS_CHANGED, <span class="keyword">this</span>.clientRemotingProcessor, <span class="keyword">null</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>.remotingClient.registerProcessor(RequestCode.RESET_CONSUMER_CLIENT_OFFSET, <span class="keyword">this</span>.clientRemotingProcessor, <span class="keyword">null</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>.remotingClient.registerProcessor(RequestCode.GET_CONSUMER_STATUS_FROM_CLIENT, <span class="keyword">this</span>.clientRemotingProcessor, <span class="keyword">null</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>.remotingClient.registerProcessor(RequestCode.GET_CONSUMER_RUNNING_INFO, <span class="keyword">this</span>.clientRemotingProcessor, <span class="keyword">null</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>.remotingClient.registerProcessor(RequestCode.CONSUME_MESSAGE_DIRECTLY, <span class="keyword">this</span>.clientRemotingProcessor, <span class="keyword">null</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>其中有个消息请求码<strong>RequestCode.CONSUME_MESSAGE_DIRECTLY</strong>，注册的便是消费者接收到消息后的处理器。<strong>registerProcessor</strong>大致流程是将<strong>ClientRemotingProcessor</strong>放入一个<strong>processorTable</strong>中（HashMap&lt;Integer/<em> request code </em>/, Pair&lt;NettyRequestProcessor, ExecutorService&gt;&gt;），然后根据不同的消息请求码，调用不同的<strong>ClientRemotingProcessor</strong>，来看下<strong>ClientRemotingProcessor</strong>的处理逻辑：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> RemotingCommand <span class="title">processRequest</span><span class="params">(ChannelHandlerContext ctx,</span></span></span><br><span class="line"><span class="function"><span class="params">    RemotingCommand request)</span> <span class="keyword">throws</span> RemotingCommandException </span>&#123;</span><br><span class="line">    <span class="keyword">switch</span> (request.getCode()) &#123;</span><br><span class="line">        <span class="keyword">case</span> RequestCode.CHECK_TRANSACTION_STATE:</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">this</span>.checkTransactionState(ctx, request);</span><br><span class="line">        <span class="keyword">case</span> RequestCode.NOTIFY_CONSUMER_IDS_CHANGED:</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">this</span>.notifyConsumerIdsChanged(ctx, request);</span><br><span class="line">        <span class="keyword">case</span> RequestCode.RESET_CONSUMER_CLIENT_OFFSET:</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">this</span>.resetOffset(ctx, request);</span><br><span class="line">        <span class="keyword">case</span> RequestCode.GET_CONSUMER_STATUS_FROM_CLIENT:</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">this</span>.getConsumeStatus(ctx, request);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">case</span> RequestCode.GET_CONSUMER_RUNNING_INFO:</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">this</span>.getConsumerRunningInfo(ctx, request);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">case</span> RequestCode.CONSUME_MESSAGE_DIRECTLY:</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">this</span>.consumeMessageDirectly(ctx, request);</span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>看一下消费方法<strong>consumeMessageDirectly()</strong>：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> RemotingCommand <span class="title">consumeMessageDirectly</span><span class="params">(ChannelHandlerContext ctx,</span></span></span><br><span class="line"><span class="function"><span class="params">    RemotingCommand request)</span> <span class="keyword">throws</span> RemotingCommandException </span>&#123;</span><br><span class="line">    <span class="keyword">final</span> RemotingCommand response = RemotingCommand.createResponseCommand(<span class="keyword">null</span>);</span><br><span class="line">    <span class="keyword">final</span> ConsumeMessageDirectlyResultRequestHeader requestHeader =</span><br><span class="line">        (ConsumeMessageDirectlyResultRequestHeader) request</span><br><span class="line">            .decodeCommandCustomHeader(ConsumeMessageDirectlyResultRequestHeader.class);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">final</span> MessageExt msg = MessageDecoder.decode(ByteBuffer.wrap(request.getBody()));</span><br><span class="line"></span><br><span class="line">    ConsumeMessageDirectlyResult result =</span><br><span class="line">        <span class="keyword">this</span>.mqClientFactory.consumeMessageDirectly(msg, requestHeader.getConsumerGroup(), requestHeader.getBrokerName());</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">null</span> != result) &#123;</span><br><span class="line">        response.setCode(ResponseCode.SUCCESS);</span><br><span class="line">        response.setBody(result.encode());</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        response.setCode(ResponseCode.SYSTEM_ERROR);</span><br><span class="line">        response.setRemark(String.format(<span class="string">"The Consumer Group &lt;%s&gt; not exist in this consumer"</span>, requestHeader.getConsumerGroup()));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> response;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>调用到<strong>MQClientInstance</strong>的<strong>consumeMessageDirectly()</strong>：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> ConsumeMessageDirectlyResult <span class="title">consumeMessageDirectly</span><span class="params">(<span class="keyword">final</span> MessageExt msg,</span></span></span><br><span class="line"><span class="function"><span class="params">    <span class="keyword">final</span> String consumerGroup,</span></span></span><br><span class="line"><span class="function"><span class="params">    <span class="keyword">final</span> String brokerName)</span> </span>&#123;</span><br><span class="line">    MQConsumerInner mqConsumerInner = <span class="keyword">this</span>.consumerTable.get(consumerGroup);</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">null</span> != mqConsumerInner) &#123;</span><br><span class="line">        DefaultMQPushConsumerImpl consumer = (DefaultMQPushConsumerImpl) mqConsumerInner;</span><br><span class="line"></span><br><span class="line">        ConsumeMessageDirectlyResult result = consumer.getConsumeMessageService().consumeMessageDirectly(msg, brokerName);</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>按照不同的消费模式，调用不同的<strong>ConsumeMessageService</strong>的<strong>consumeMessageDirectly()</strong>，如果是顺序消费，调用<strong>ConsumeMessageOrderlyService</strong>，否则调用<strong>ConsumeMessageConcurrentlyService</strong>。按照我们上面的示例来说，因为注册了<strong>MessageListenerConcurrently</strong>，所以这里调用的是<strong>ConsumeMessageConcurrentlyService</strong>的<strong>consumeMessageDirectly()</strong>：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> ConsumeMessageDirectlyResult <span class="title">consumeMessageDirectly</span><span class="params">(MessageExt msg, String brokerName)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 省略上面部分代码，只展示核心处理部分</span></span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        ConsumeConcurrentlyStatus status = <span class="keyword">this</span>.messageListener.consumeMessage(msgs, context);</span><br><span class="line">        <span class="keyword">if</span> (status != <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">switch</span> (status) &#123;</span><br><span class="line">                <span class="keyword">case</span> CONSUME_SUCCESS:</span><br><span class="line">                    result.setConsumeResult(CMResult.CR_SUCCESS);</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> RECONSUME_LATER:</span><br><span class="line">                    result.setConsumeResult(CMResult.CR_LATER);</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">default</span>:</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            result.setConsumeResult(CMResult.CR_RETURN_NULL);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">catch</span> (Throwable e) &#123;</span><br><span class="line">        result.setConsumeResult(CMResult.CR_THROW_EXCEPTION);</span><br><span class="line">        result.setRemark(RemotingHelper.exceptionSimpleDesc(e));</span><br><span class="line"></span><br><span class="line">        log.warn(String.format(<span class="string">"consumeMessageDirectly exception: %s Group: %s Msgs: %s MQ: %s"</span>,</span><br><span class="line">            RemotingHelper.exceptionSimpleDesc(e),</span><br><span class="line">            ConsumeMessageConcurrentlyService.<span class="keyword">this</span>.consumerGroup,</span><br><span class="line">            msgs,</span><br><span class="line">            mq), e);</span><br><span class="line">    &#125;</span><br><span class="line">    result.setSpentTimeMills(System.currentTimeMillis() - beginTime);</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>根据用户MessageListener的逻辑，来返回不同的消费结果。</p>
<h4 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h4><p>&emsp;&emsp;以上简单地分析了下RocketMQ在整个Push模式下的Consumer启动、消息拉取、消费的过程，并且得知了Push模式下实质上也是一个Pull消息的过程。至于Pull模式（DefaultMQPullConsumer）就不再讨论，实质上处理流程和Push类似，只是Pull模式下，用户需要自己根据MessageQueue去拉取消息并根据返回结果记录offset，判断当前消息消费的情况，而这些东西在Push模式下都被封装了，简单易用。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/RocketMQ之旅/" rel="tag"># RocketMQ之旅</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/10/25/RocketMQ之旅（三）client之Producer/" rel="next" title="RocketMQ之旅（三）client之Producer">
                <i class="fa fa-chevron-left"></i> RocketMQ之旅（三）client之Producer
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/11/17/算法国（一）排序/" rel="prev" title="算法国（一）排序">
                算法国（一）排序 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">matrix</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">47</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">18</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#官方示例"><span class="nav-number">1.</span> <span class="nav-text">官方示例</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#DefaultMQPushConsumerImpl启动"><span class="nav-number">2.</span> <span class="nav-text">DefaultMQPushConsumerImpl启动</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#拉取消息"><span class="nav-number">3.</span> <span class="nav-text">拉取消息</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#消费消息"><span class="nav-number">4.</span> <span class="nav-text">消费消息</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#总结"><span class="nav-number">5.</span> <span class="nav-text">总结</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">matrix</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>
<div class=BbeiAn-info">
  浙ICP备 -
  <a href="http://www.beian.miit.gov.cn">18040498号</a>
  </a>
</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  

    
      <script id="dsq-count-scr" src="https://luyun.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'http://yoursite.com/2018/11/12/RocketMQ之旅（四）client之Consumer/';
          this.page.identifier = '2018/11/12/RocketMQ之旅（四）client之Consumer/';
          this.page.title = 'RocketMQ之旅（四）client之Consumer';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://luyun.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  





  

  

  

  
  

  

  

  

</body>
</html>
